Public Partial Class EditRole
    Inherits System.Web.UI.Page
    Private allUsers As MembershipUserCollection = System.Web.Security.Membership.GetAllUsers

    Private Sub EditRole_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            ltRoleName.Text = Request.QueryString("RoleName")
            Dim alphabet As String() = _
            "A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z;All".Split(";")
            rptAlphabet.DataSource = alphabet
            rptAlphabet.DataBind()
            gvRoleUsers.DataSource = BindUsersInRole(True)
            gvRoleUsers.DataBind()
        End If
    End Sub

    Private Function BindUsersInRole(ByVal reloadAllUsers As Boolean) As System.Web.Security.MembershipUserCollection
        If reloadAllUsers Then

        End If
        Dim dtReturnTable As New System.Web.Security.MembershipUserCollection
        For Each Item As String In System.Web.Security.Roles.GetUsersInRole(ltRoleName.Text)
            Dim drNewUser As System.Web.Security.MembershipUser = System.Web.Security.Membership.GetUser(Item, False)
            dtReturnTable.Add(drNewUser)
        Next
        Return dtReturnTable
    End Function

    Private Sub BindUsers(ByVal reloadAllUsers As Boolean)
        If reloadAllUsers Then
            allUsers = System.Web.Security.Membership.GetAllUsers
        End If

        Dim users As MembershipUserCollection = Nothing

        Dim searchText As String = ""
        If Not String.IsNullOrEmpty(gvRoleUsers.Attributes("SearchText")) Then
            searchText = gvRoleUsers.Attributes("SearchText")
        End If

        Dim searchByEmail As Boolean = False
        If Not String.IsNullOrEmpty(gvRoleUsers.Attributes("SearchByEmail")) Then
            searchByEmail = Boolean.Parse(gvRoleUsers.Attributes("SearchByEmail"))
        End If

        If searchText.Length > 0 Then
            If searchByEmail Then
                users = System.Web.Security.Membership.FindUsersByEmail(searchText)
            Else
                users = System.Web.Security.Membership.FindUsersByName(searchText)
            End If
        Else
            users = allUsers
        End If

        gvRoleUsers.DataSource = users
        gvRoleUsers.DataBind()
    End Sub
    Protected Sub rptAlphabet_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptAlphabet.ItemCommand
        gvRoleUsers.Attributes.Add("SearchByEmail", Boolean.FalseString)

        If e.CommandArgument.ToString.Length = 1 Then
            gvRoleUsers.Attributes.Add("SearchText", e.CommandArgument.ToString + "%")
            BindUsers(False)
        Else
            gvRoleUsers.Attributes.Add("SearchText", "")
            BindUsers(False)
        End If
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim searchByEmail As Boolean = (ddlSearchTypes.SelectedValue = "E-mail")
        gvRoleUsers.Attributes.Add("SearchText", txtSearchText.Text)
        gvRoleUsers.Attributes.Add("SearchByEmail", searchByEmail.ToString)
        BindUsers(False)
    End Sub

    Protected Sub gvwUsers_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvRoleUsers.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            If Not e.Row.DataItem Is Nothing Then
                If System.Web.Security.Roles.IsUserInRole(CType(e.Row.DataItem, System.Web.Security.MembershipUser).UserName, ltRoleName.Text) Then
                    CType(e.Row.FindControl("chkInRole"), CheckBox).Checked = True
                Else
                    CType(e.Row.FindControl("chkInRole"), CheckBox).Checked = False
                End If
            End If
        End If
    End Sub
End Class